#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h> 
int main()
{
  //环境变量的指针声明
  extern char** environ;
  printf("我是父进程，我的pid是：%d\n",getpid());
  pid_t id = fork();
  if(id == 0)
  {
    //child
    //我们让子进程执行全新的程序，以前是执行父进程的代码片段
    printf("我是子进程，我的pid是%d\n",getpid());  
    char *const env[] = {
        (char*)"MYPATH=youcanseeme!",NULL };
    //e：添加环境变量给目标进程是覆盖式的
    execle("./mycmd","mycmd",NULL,env); 
    //execl("./mycmd","mycmd",NULL);

    //execl("/usr/bin/bash","bash","test.sh",NULL);
    //execl("/usr/bin/python3","python3","test.py",NULL);
    //execl("./mycmd","mycmd",NULL);
    
   // execl("/home/Lxy/code/linux-code/practice/10-28/mycmd",
   //     "mycmd",NULL);
   /// char* const argv_[] = {
   //   (char*) "ls",
   //   (char*) "-l",
   //   (char*) "-a",
   //   NULL
   // };
   // execvp("ls",argv_);
    //execlp("ls","ls","-a","-l",NULL);
    //这里出现的两个ls,可以省略吗，含义一样吗？
    //不能省略 含义不一样


    //char* const argv_[] = {
    //  (char*) "ls",
    //  (char*) "-l",
    //  (char*) "-a",
    //  NULL
    //};
    //execv("/usr/bin/ls",argv_);

    //execl("/usr/bin/ls","ls","-a","-l",NULL);
    exit(1);//只要执行了exit 意味着execl函数失败
  }
  //这里就是父进程
  int status = 0;
  int ret = waitpid(id,&status,0);
  if(ret == id)
  {
    printf("父进程等待成功\n");
  }



  // ls -a -l
  //execl("/usr/bin/ls","ls","-l","-a",NULL);
  //top
  //int ret = execl("/usr/bin/lsssss","ls","-l","-a",NULL);
  //printf("我执行完毕了，我的pid是：%d,ret = %d\n",getpid(),ret);
  return 0;
}
